package com.bobi.tfca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bobi.tfca.model.constant.RedisConstant;
import com.bobi.tfca.model.entity.AuditEntity;
import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.entity.MemberProfileEntity;
import com.bobi.tfca.model.enums.AuditCategoryEnum;
import com.bobi.tfca.model.enums.AuditStatusEnum;
import com.bobi.tfca.model.enums.MemberStatusEnum;
import com.bobi.tfca.model.enums.SubAuditItemTypeEnum;
import com.bobi.tfca.model.vo.MemberProfileVO;
import com.bobi.tfca.service.AuditService;
import com.bobi.tfca.service.MemberManageService;
import com.bobi.tfca.service.MemberProfileService;
import com.bobi.tfca.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 李文发
 * @date 2024/4/25
 */
@Service
public class MemberMangageServiceImpl implements MemberManageService {


    @Autowired
    private AuditService auditService;

    @Autowired
    private MemberProfileService memberProfileService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Integer registerMember(MemberProfileVO memberProfileVO, String code) {
        // 从redis中获得账号的，邮件验证码
        String account = memberProfileVO.getTelephone();
        String redisCode = stringRedisTemplate.opsForValue().get(RedisConstant.USER_CAPTCHA_PREFIX + account);
        // 比对redisCode
        if (redisCode == null || (!redisCode.equals(code))) {
            return 1;
        }
        String telephone = memberProfileVO.getTelephone();
        MemberEntity registedMember = memberService.getOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getTelephone, telephone));
        if (registedMember != null) {
            return 2;
        }
        // 转换profile对象
        MemberProfileEntity profileEntity = MemberProfileEntity.ofMemberProfileVO(memberProfileVO);
        // 创建MemberEntity
        Integer memberId = generateMemberId();
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setMemberId(memberId);
        memberEntity.setMemberName(profileEntity.getMemberName());
        memberEntity.setMemberStatus(MemberStatusEnum.WAITING_AUDIT.getCode());
        memberEntity.setBirthday(profileEntity.getBirthday());
        memberEntity.setEmail(profileEntity.getEmail());
        memberEntity.setTelephone(profileEntity.getTelephone());
        memberEntity.setPicture(profileEntity.getPicture());
        memberEntity.setSex(profileEntity.getSex());
        memberService.save(memberEntity);

        // 会员档案保存
        profileEntity.setMemberId(memberEntity.getMemberId());
        memberProfileService.save(profileEntity);

        // 生成入会申请
        AuditEntity audit = new AuditEntity();
        audit.setApplyTime(LocalDateTime.now());
        audit.setItemId(memberEntity.getMemberId());
        audit.setCategory(AuditCategoryEnum.MEMBER_APPLY.getCode());
        audit.setAuditStatus(AuditStatusEnum.NOT_AUDITED.getCode());
        audit.setSubType(SubAuditItemTypeEnum.REGISTER_APPLY.getCode());
        auditService.save(audit);
        return 0;
    }

    // 后续可以换成Redis生成Id
    private Integer generateMemberId() {
        int uid = 0;
        MemberEntity entity = null;
        do {
            uid = (int) (System.currentTimeMillis() & 0xfffffff)  % 10000000;
            uid++;
            entity = memberService.getById(uid);
        } while (entity != null);
        return uid;
    }
}
